#!/bin/sh

#Cleanup the graphml before you begin.
rm -r 'files/graph.graphml'

#Step 1: Get the names of the research staff from ILPS
curl http://ilps.science.uva.nl/people |
sed -n '/<h2>research staff<\/h2>/,/<h2>support staff<\/h2>/p'|
grep 'View user profile' |
sed 's/<[^>]*>//g'| 
sort >> files/namesILPS.txt

#Step 2: Get search urls on DBLP
while read LINE; do
echo $LINE|
sed "s%^%http://dblp.uni-trier.de/search/author?author=%" | 
sed "s/ /+/g"  >> files/searchurl.txt
done < "files/namesILPS.txt"

#Step 3: Get the real urls. Problem: Some people have two names.
while read LINE; do 
curl $LINE|
grep -o 'http://dblp.uni-trier.de/db/indices/a-tree/./[^"]*.html'>> files/urlsDBLP.txt
done < "files/searchurl.txt"

#Step 4: Get the names of the authors as they are on DBLP.
while read LINE;do
echo $LINE|
sed 's%http://dblp.uni-trier.de/db/indices/a-tree/%%  ; s%/xc%%'|
sed 's/.html//g'>> files/namesDBLP.txt
done < "files/urlsDBLP.txt"

#Step 5: Create all nodes using the DBLP names.
while read LINE; do
echo "<node id=\"$LINE\">\n
	<data key=\"n1\">\"$LINE\"</data>\n
</node>" |
sed " ; s%[a-z]/%% ; s%[a-z]/%%" | 
sed 's/:/ /g'>> files/nodes.xml
done < "files/namesDBLP.txt"

#Step 6.1: Create all edges by putting the DBLP names in a link. These edges
#        still have names outside ILPS in it, and double edges.
while read LINE; do
curl http://dblp.uni-trier.de/rec/pers/$LINE/xc |
grep '<author' |
sed    "s%author%edge source=\"$LINE\"% ; 
	s%urlpt%target% ; 
	s%count=\"%><data key=\"e1\">%" |
sed "s%\">[ ]*[A-Z].*%</data></edge>%" >>files/wrongedges.xml
done <"files/namesDBLP.txt"

#Step 6.2: Filter out the names that are not in ILPS and rename names 
#        from b/Bouwer:Anders to Bouwer Anders

rightnames=`cat files/namesDBLP.txt | tr '\n' '|' |sed 's/^/(/; s/|$/)/'`
 
cat files/wrongedges.xml |grep -P "target=\"$rightnames\"" | 
sed " ; s%[a-z]/%% ; s%[a-z]/%%" | 
sed 's/:/ /g' >> files/wrong2edges.xml

#Step 6.3: Filter out the dubbel edges like A-B is the same as B-A.
while read LINE; do
source=`echo $LINE | grep -o 'source="[^"]*' | sed 's/source=\"//g'`
target=`echo $LINE | grep -o 'target="[^"]*' | sed 's/target=\"//g'`

if [ "$source" \< "$target" ]  
  then  
     echo $LINE  |
     sed "s/<edge/<edge id= \"$source - $target\"/g" |
     sed "s/\" - \"/-/">> files/edges.xml
fi
done < "files/wrong2edges.xml"

#Step 7: Final step, make the graphml file

#Step 7.1: Fill the header of the graphml file.
echo "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">
<graph id=\"Coauthors\" edgedefault=\"undirected\">
<desc>
<meta debatetitle=\"Coauthors\"/>
</desc>
<key id=\"n1\" for=\"node\" attr.name=\"name\" attr.type=\"string\"/>
<key id=\"e1\" for=\"edge\" attr.name=\"number of publications\" attr.type=\"int\"/>" >>files/graph.graphml

#Step 7.2: Fill the graphml file with the nodes.
cat files/nodes.xml >> files/graph.graphml

#Step 7.3: Fill the graphml file with the edges.
cat files/edges.xml >> files/graph.graphml

#Step 7.4: Ending the graphml file.
echo "</graph>\n</graphml>" >> files/graph.graphml

#remove all tempory files.
rm -r 'files/edges.xml'
rm -r 'files/nodes.xml'
rm -r 'files/wrongedges.xml'
rm -r 'files/searchurl.txt'
rm -r 'files/namesDBLP.txt'
rm -r 'files/namesILPS.txt'
rm -r 'files/wrong2edges.xml'
rm -r 'files/urlsDBLP.txt'


